package cn.leelei.springmvc.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.leelei.springmvc.entity.User;
/**
 * 注解实现
 * @author Administrator
 * 
 * 窄化请求映射 在类上添加  @RequestMapping("/login.do") 用于窄化请求
 *
 */
@Controller
@RequestMapping("/login.do")
public class LoginController {
	/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 * 设置默认支持请求参数提交方式
	 * method=RequestMethod.POST 
	 * 多个请求参数表示方法
	 * method={RequestMethod.POST ,RequestMethod.GET})
	 * 方法1.支持原生servlet api
	 * 
	 * 
	 */

	@RequestMapping( value="/login.do", method={RequestMethod.POST ,RequestMethod.GET})//这里的地址需要和请求的页面action一致
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//设置字符集
		request.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		//获得页面的提交参数
		String userName = request.getParameter("userName");
		String passWord = request.getParameter("passWord");
		
		
		ModelAndView mv= new ModelAndView();
		
		if("admin".equals(userName) && "admin".equals(passWord)){
			mv.setViewName("success");
			mv.addObject("list",userName+"方法2");//最终会将数据放在request中
		}else{
			mv.setViewName("fail");
		}
		return mv;
	}
	/**
	 * 
	 * 方法2. 用户输入的参数名和提交参数名一致
	 * @param userName
	 * @param passWord
	 * @return
	 * 实用于登录验证
	 */
	@RequestMapping("/login2.do")
	public ModelAndView login(String userName,String passWord){
		
		ModelAndView mv= new ModelAndView();
		if("admin".equals(userName) && "admin".equals(passWord)){
			mv.setViewName("success");
			mv.addObject("list",userName);//最终会将数据放在request中
		}else{
			mv.setViewName("fail");
		}
		return mv;
		
	}
	
	/**
	 * 支持简单的 pojo对象 
	 *  适用于 注册
	 * @param userName
	 * @param passWord
	 * @return
	 */
	@RequestMapping("/login3.do")
	public ModelAndView login3(User user){
		
		ModelAndView mv= new ModelAndView();
		if("admin".equals(user.getUserName()) && "admin".equals(user.getPassWord())){
			mv.setViewName("success");
			mv.addObject("list",user.getUserName());//最终会将数据放在request中
		}else{
			mv.setViewName("fail");
		}
		return mv;
		
	}
	/**
	 * 批量添加信息
	 * @param user
	 * @return
	 * 
	 * 集合类需要使用包装类
	 */
	
	@RequestMapping("/BatchAdd.do")
	public ModelAndView BatchAdd(User users){
		System.err.println("批量添加信息"+users);
		
		ModelAndView mv= new ModelAndView();
		mv.setViewName("success");
		return mv;
		
	}
	
	@RequestMapping("/Register.do")
	public ModelAndView Register(User users){
		System.err.println("批量添加信息"+users.toString());
		
		ModelAndView mv= new ModelAndView();
		mv.setViewName("success");
		return mv;
		
	}



}
